﻿var app = angular.module('app', ['angular.jquery']);
var item = null;
var flag = false;
var objCurrent = null;
app.directive('ckEditor', [function () {
    return {
        require: '?ngModel',
        link: function ($scope, elm, attr, ngModel) {

            var ck = CKEDITOR.replace(elm[0]);

            ck.on('pasteState', function () {
                $scope.$apply(function () {
                    ngModel.$setViewValue(ck.getData());
                });
            });

            ngModel.$render = function (value) {
                ck.setData(ngModel.$modelValue);
            };
        }
    };
}])

app.controller('DemoCtrl', [
    '$scope', '$http',
    'TestDialogService',
function ($scope, $http, TestDialogService) {
    'use strict';
    objCurrent = Editor.getItem("btv");
    $scope.showAdd = function () {
        if (objCurrent != null) {
            return objCurrent.isadd;
        }
        return false;
    }
    $scope.showEdit = function () {
        if (objCurrent != null) {
            return objCurrent.edit;
        }
        return false;
    }
    $scope.showDelete = function () {
        if (objCurrent != null) {
            return objCurrent.isdelete;
        }
        return false;
    }

    $http.get(Editor.Url + "/btv/GetAll")
     .success(function (response) {
         $scope.talks = response;
         console.dir(response);
     });

    $scope.loadData = function () {
        $http.get(Editor.Url + "/btv/GetAll")
                .success(function (response) {
                    $scope.talks = response;
        });
    }

    $scope.delete = function (id) {
        var a = confirm("Bạn có thực sự muốn xóa dòng này?");
        if (a === false)
            return;
        $http.delete(Editor.Url + "/api/ThuongVuAPI/" + id).success(function (data, status, headers) {
            $scope.loadData();
        });
    }

    $scope.testDialog = function (_flag, _item) {
        item = _item;
        flag = _flag;
        TestDialogService.openDialog({
            testValue: 'test'
        }).then(function (result) {
            if (result.ok) {
                var mota = $("#moTaBTV").val();    
                result.obj.moTaBTV = mota;
                if (flag) {
                    console.dir(result.obj);
                    $http.put(Editor.Url + "/api/ThuongVuAPI/" + result.obj.iD, result.obj).success(function (data, status, headers) {
                        if (status == 200) {
                            alert("cập nhập thành công");
                        }
                    });
                }
                else {
                    $http.post(Editor.Url + "/api/ThuongVuAPI", result.obj).success(function (data, status, headers) {
                        $scope.loadData();
                    });
                }
            }
        });

    };

}]);



app.controller('DialogCtrl', [
    '$scope',
    'TestDialogService',
function ($scope, TestDialogService) {
    'use strict';

    $scope.obj = null;
    $scope.onOpen = function () {
        if (flag)
            $scope.obj = item;
        if ($scope.obj != null) {                            
            $('#moTaBTV').val($scope.obj.moTaBTV).blur();
        }
        else {

        }
    };

    $scope.$parent.onOk = function () {
        TestDialogService.closeDialog({ ok: true, obj: $scope.obj });
    };

    $scope.onCancel = function () {
        TestDialogService.closeDialog({ ok: false });
    };

    $scope.beforeClose = function () {
        return "";//confirm('Really close?')
    };

}]);